package com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.person;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.helper.AccountStore;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.event.HomeEvent;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.DynamicListParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.UserInfoParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DynamicResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.UserResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.DynamicContract;
import com.kaiwukj.android.ufamily.utils.upload.OnUploadListener;
import com.kaiwukj.android.ufamily.utils.upload.UploadBean;
import com.kaiwukj.android.ufamily.utils.upload.UploadUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

public class PersonDynamicPresenter extends BasePresenter<DynamicContract.Model, DynamicContract.PersonIndexView> {

    public static final int USER_BG_UPDATE = 1;
    public static final int ATTENTION = 10;
    public static final int ATTENTION_CANCEL = 11;
    public static final int BLOCK_USER = 12;
    public static final int REPORT_USER = 13;

    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public PersonDynamicPresenter(DynamicContract.Model model, DynamicContract.PersonIndexView rootView) {
        super(model, rootView);
    }

    public void fetchList(int startId, int pageSize, int userId) {
        DynamicListParams params = new DynamicListParams();
        params.setPageSize(pageSize);
        params.setStartId(startId);
        params.setUserId(userId);
        mModel.fetchDynamic(params)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<DynamicResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull ListResp<DynamicResult> result) {
                        mRootView.onGetPersonDynamic(result.getList());
                    }
                });
    }

    public void fetchInfo(int userId) {
        mModel.fetchPersonIndexInfo(userId)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<UserResult>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull UserResult result) {
                        mRootView.onGetPersonInfo(result);
                    }
                });
    }

    public void updateBgImg(String imgUrl) {
        UserInfoParams params = new UserInfoParams();
        params.setBackImg(imgUrl);
        mModel.updateUserInfo(params)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, USER_BG_UPDATE) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        UserResult userResult = AccountStore.getUserResult();
                        userResult.setBackImg(imgUrl);
                        AccountStore.updateUserCache(userResult);
                    }
                });
    }

    public void uploadFile(String filePath) {
        mModel.getQiNiuToken()
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<String>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull String result) {
                        UploadUtils.getInstance().upload(filePath, result, UploadUtils.IMAGE, new OnUploadListener() {
                            @Override
                            public void onUploadImage(UploadBean result) {
                                updateBgImg(result.getNormalUrl());
                            }

                            @Override
                            public void onUploadImages(List<UploadBean> results) {

                            }

                            @Override
                            public void onError(int errCode, String errMsg) {

                            }
                        });
                    }
                });
    }

    public void attention(int userId) {
        mModel.attention(userId)
                .doOnSubscribe(act -> mRootView.showLoading("正在请求..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, ATTENTION) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {

                    }
                });
    }

    public void cancelAttention(int userId) {
        mModel.cancelAttention(userId)
                .doOnSubscribe(act -> mRootView.showLoading("正在请求..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, ATTENTION_CANCEL) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {

                    }
                });
    }

    public void blockUser(int userId) {
        mModel.shield(userId, true, true)
                .doOnSubscribe(act -> mRootView.showLoading("正在请求..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, BLOCK_USER) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.showMessage("已屏蔽");
                        EventBus.getDefault().post(new HomeEvent(HomeEvent.USER_BLOCK));
                    }
                });
    }

    public void reportUser() {
        //  mModel.report()
    }
}
